﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zone;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using Iyond.Utility;
using System.Net;
using System.Xml;
namespace SmsSend
{
    static class Program
    {
        static string isdone = "";
        public static string IsDone = "";
        private const int WS_SHOWNORMAL = 1;
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        internal static ApplicationContext context = new ApplicationContext(new Login());
        //internal static ApplicationContext context = new ApplicationContext(new Login());

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {           
            System.Diagnostics.Process instance = GetRunningInstance();
            if (instance == null)
            {
                Test();
                //if (isdone == "ok")
                //{
                //    MessageBox.Show("更新成功，请重新启动程序！");
                //    return;
                //}
                                  
                //************第一种跳转窗口的方法******************

                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Login login = new Login();
                //Application.Run(login);
                //if (Login.flag == true)
                //{
                //    Application.Run(new MainFm());
                //}      
                //*************第二种跳转窗口的方法*****************
                Application.EnableVisualStyles();                
                Application.Run(context);
                //**************************************************

            }
            else
            {
                HandleRunningInstance(instance);
            }
        }

        //测试更新
        public static void Test()
        {
            AutoUpdater au = new AutoUpdater(); 
            try
            {
                au.Update();
            }
            catch (WebException exp)
            {
                MessageBox.Show(String.Format("无法找到指定资源\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (XmlException exp)
            {
                MessageBox.Show(String.Format("下载的升级文件有错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (NotSupportedException exp)
            {
                MessageBox.Show(String.Format("升级地址配置错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ArgumentException exp)
            {
                MessageBox.Show(String.Format("下载的升级文件有错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception exp)
            {
                MessageBox.Show(String.Format("升级过程中发生错误\n\n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


        public static Process GetRunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);

            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                        return process;
            }
            return null;
        }
        public static void HandleRunningInstance(Process instance)
        {
            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
            SetForegroundWindow(instance.MainWindowHandle);
        }
    }
}
